package cn.itsource.hrm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration //交给spring管理
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    //1 配置UserDetailsService
    //2 配置加密密码器
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    //3 配置授权规则

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequests()
                //自己实现了颁发token登录，就不需要原来的登录了
//                .antMatchers("/login").permitAll() //登录放行
                .antMatchers("/loginUser/login").permitAll() //登录放行
                .antMatchers("/login/refresh").permitAll() //登录放行
                .anyRequest().authenticated()
                .and().formLogin()
//                .successForwardUrl("/loginSuccess")
                .and().csrf().disable()
                .logout().permitAll(); //退出放行
    }


    //由于oauth的配置需要authenticationManager，所以写一个访问调用服务authenticationManager获取一个AuthenticationManager交给spring管理
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}
